<?php
define("IS_MAIN", true);
require "./modules/set.php";


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

require "./modules/mail.php";

header("Content-Type: application/json;charset=utf8");
if ($_SERVER["REQUEST_METHOD"] != "GET") {
	echo '{"code":"error","message":"请求方法不正确"}';
	exit;
}
if ((isset($_COOKIE["htqw_share_login_password"]) && isset($_COOKIE["htqw_share_login_id"])) == false) {
	echo '{"code":"error","message":"缺少COOKIE"}';
	exit;
}
$id = $_COOKIE["htqw_share_login_id"];
$password = $_COOKIE["htqw_share_login_password"];
$conn = mysqli_connect($app["sql"]["host"], $app["sql"]["user"], $app["sql"]["password"], $app["sql"]["dbname"], $app["sql"]["port"]);
if ($conn->connect_errno) {
	echo '{"code":"error","message":"数据库链接失败"}';
	exit;
}
$stmt = $conn->prepare("select * from users where id=?;");
$aaaaaaa = $id . "";
$stmt->bind_param("s", $aaaaaaa);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 0) {
	mysqli_close($conn);
	echo '{"code":"error","message":"没有该用户"}';
	exit;
}
$row = $result->fetch_assoc();
if ($row["password"] != $password) {
	mysqli_close($conn);
	echo '{"code":"error","message":"密码错误！"}';
	exit;
}

send_mail(new PHPMailer, "登录", "<h1>您的账户“" . $row["name"] . "”(id：" . $id . ")在IP为" . $_SERVER["REMOTE_ADDR"] . "登录</h1>", $row["email"], $app);
setcookie("htqw_share_user", json_encode($row), time() + 60 * 24 * 30, "/");
mysqli_close($conn);
echo '{"code":"success"}';
